<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="所属银行" prop="deptId">
        <el-tree-select
          v-model="queryParams.deptId"
          :data="deptList"
          :props="defaultProps"
          check-strictly
          node-key="id"
          class="!w-240px"
          placeholder="请选择归属银行"
          clearable="true"
        />
      </el-form-item>
      <el-form-item label="年份" prop="year">
        <el-date-picker
          v-model="queryParams.year"
          type="year"
          value-format="YYYY"
          placeholder="请选择年份"
        />
      </el-form-item>
      <el-form-item label="季度" prop="quarter">
        <el-select
          v-model="queryParams.quarter"
          placeholder="请选择季度"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions('circ_quarter')"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上报时间" prop="statsDates">
        <el-date-picker
          v-model="queryParams.statsDates"
          value-format="YYYY-MM-DD"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['circ:data-stats:reportexcel']"  >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" show-summary empty-text="无记录" :show-overflow-tooltip="true">
      <el-table-column label="序号" type="index" :index="indexMethod"  />
      <el-table-column label="市县" align="center" prop="cityName" />
      <el-table-column label="乡镇" align="center" prop="townName" />
      <el-table-column label="村/社区" align="center" prop="name" />
      <el-table-column label="户数" align="center" prop="familyNum" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="余额" align="center" prop="balance" />

    </el-table>
  </ContentWrap>

</template>

<script setup lang="ts">
import { getIntDictOptions } from '@/utils/dict'
import { DataReportApi, DataReportVO } from '@/api/circ/dataReport'
import { defaultProps, handleTree } from '@/utils/tree'
import * as DeptApi from '@/api/system/dept'
import download from '@/utils/download'

/** 数据统计主 列表 */
defineOptions({ name: 'DataStats' })

const message = useMessage() // 消息弹窗
const loading = ref(true) // 列表的加载中
const list = ref<DataReportVO[]>([]) // 列表的数据
const exportLoading = ref(false) // 导出的加载中
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  deptId: undefined,
  year: undefined,
  quarter: undefined,
  statsDate: [],
})
const queryFormRef = ref() // 搜索的表单
const deptList = ref<Tree[]>([]) // 树形结构

const indexMethod = (index: number) => {
  return index +1
}

/** 查询汇总数据列表 */
const getList = async () => {
  loading.value = true
  try {
    const data: any = await DataReportApi.getDataReport(queryParams)
    //console.log(data);
    list.value = data
   /* total.value = data.length*/
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DataReportApi.getDataReportExcel(queryParams)
    download.excel(data, '数据统计汇总.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async() => {
  getList();
  const deptParams = reactive({type: 3  })
  deptList.value = handleTree(await DeptApi.getSimpleDeptListAppApi(deptParams))
})
</script>
